package ms.maplestory.boot.packets.universal;

import ms.maplestory.boot.client.character.Char;
import ms.maplestory.boot.client.character.Inventory.Item;
import ms.maplestory.boot.connection.OutPacket;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class AvatarLook {

    public static OutPacket encode(OutPacket out, Char chr, boolean mega) {
        out.encodeByte(chr.getStat().getGender());
        out.encodeByte(chr.getStat().getSkin());
        out.encodeInt(chr.getStat().getFace());
        out.encodeByte(mega);
        out.encodeInt(chr.getStat().getHair());

        List<Item> equipped = chr.getEquippedInventory().getItems();
        Map<Byte, Integer> hairEquip = new LinkedHashMap<>();
        Map<Byte, Integer> unseenEquip = new LinkedHashMap<>();
        int weaponStickerId = 0;
        for (Item equip : equipped) {
            if (equip.getPosition() < -128) {
                continue;
            }
            byte pos = (byte) (equip.getPosition() * -1);
            if (pos == -111) {
                weaponStickerId = equip.getItemId();
            }
            if (pos < 100 && hairEquip.get(pos) == null) {
                hairEquip.put(pos, equip.getItemId());
            }
            if ((pos > 100 || pos == -128) && pos != 111) {
                pos = (byte) (pos == -128 ? 28 : pos - 100);
                if (hairEquip.get(pos) != null) {
                    unseenEquip.put(pos, equip.getItemId());
                }
                hairEquip.put(pos, equip.getItemId());
            } else if (hairEquip.get(pos) != null) {
                unseenEquip.put(pos, equip.getItemId());
            }
        }
        hairEquip.forEach((p, i) -> {
            out.encodeByte(p);
            out.encodeInt(i);
        });
        out.encodeByte(-1);
        unseenEquip.forEach((p, i) -> {
            out.encodeByte(p);
            out.encodeInt(i);
        });
        out.encodeByte(-1);
        out.encodeInt(weaponStickerId);
        for (int i = 0; i < 3; i++) { // petItemId
            out.encodeInt(0);
        }
        return out;
    }

}
